/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 * <p>
 * <p>
 * file description.
 *
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-07-30              zhihuazhang         Create it.
 * <p>
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-07-30              zhihuazhang         Create it.
 */

/**
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-07-30              zhihuazhang         Create it.
 *
 */

package com.cart.musicdemo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.widget.Toast;

import com.cart.musicdemo.R;

public class UDiskReceiver extends BroadcastReceiver {

    private LoadApp loadApp;

    private StopApp stopApp;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // USB插入识别
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)
            || Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
            // 插入检测成功后，就回调LoadApp.init()方法进行app初始化
            Toast.makeText(context, R.string.action_media_mounted, Toast.LENGTH_SHORT).show();
            loadApp.init();
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)
            || Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
            // 拔出
            Toast.makeText(context, R.string.action_media_unmounted, Toast.LENGTH_SHORT).show();
            stopApp.destroy();
        }
    }

    public void setStopApp(StopApp stopApp) {
        this.stopApp = stopApp;
    }

    public void setLoadApp(LoadApp loadApp) {
        this.loadApp = loadApp;
    }

    public interface LoadApp {
        void init();
    }

    public interface StopApp {
        void destroy();
    }
}
